package com.chookss.home.audit.shop;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.chookss.R;
import com.chookss.Urls;
import com.chookss.base.BaseViewHolderEx;
import com.chookss.home.audit.adapter.ShopDetailAdapter;
import com.chookss.home.entity.ReimbursementDetailEntity;
import com.chookss.tools.ClearMoreZeroUtil;
import com.chookss.tools.GlideUtils;
import com.chookss.tools.MyEvent;
import com.chookss.tools.ShopEvent;
import com.chookss.tools.Utils;
import com.chookss.view.RecycleViewDivider;
import com.chookss.view.RoundCornerImageView;
import com.lvgroup.hospital.base.BaseAct;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.jetbrains.annotations.Nullable;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ShopChoiceActivity extends BaseAct {
//选择物品

    @BindView(R.id.rvView)
    RecyclerView recyclerView;
    @BindView(R.id.srlView)
    SmartRefreshLayout smrRf;
    @BindView(R.id.common_title_txt)
    TextView common_title_txt;
    @BindView(R.id.common_right_txt)
    TextView common_right_txt;
    private ShopChoiceTypeAdapter adapter;
    private ShopDetailAdapter shopChoiceAdapter;
    private ArrayList<ReimbursementDetailEntity.GoodsInfosBean> list = new ArrayList<>();
    private ArrayList<ReimbursementDetailEntity.GoodsInfosBean> list2 = new ArrayList<>();
    private int type=0;
    private View emptyView;
    private TextView tvNone;//空布局;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_shop_choice);
        ButterKnife.bind(this);
    }

    @Override
    public void initData() {
        initState();
        common_title_txt.setText("关联物品");
        type=getIntent().getIntExtra("type",0);
        recyclerView.addItemDecoration(new RecycleViewDivider(this, LinearLayoutManager.HORIZONTAL, Utils.dip2px(this, 12f), getResources().getColor(R.color.main_bg)));
        recyclerView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        ((SimpleItemAnimator) recyclerView.getItemAnimator()).setSupportsChangeAnimations(false);
        if(type==0){
            common_right_txt.setText("添加");
            common_right_txt.setTextColor(getResources().getColor(R.color.main_color));
            common_right_txt.setVisibility(View.VISIBLE);
            list.addAll((List<ReimbursementDetailEntity.GoodsInfosBean>) getIntent().getSerializableExtra("data"));
            adapter = new ShopChoiceTypeAdapter(R.layout.item_shop_choice, this, list);
            recyclerView.setAdapter(adapter);

        }else{
            list2.addAll((List<ReimbursementDetailEntity.GoodsInfosBean>) getIntent().getSerializableExtra("data"));
            shopChoiceAdapter = new ShopDetailAdapter(R.layout.item_shop_choice, this, list2);
            recyclerView.setAdapter(shopChoiceAdapter);
        }
        recyclerView.getItemAnimator().setAddDuration(0);
        recyclerView.getItemAnimator().setChangeDuration(0);
        recyclerView.getItemAnimator().setMoveDuration(0);
        recyclerView.getItemAnimator().setRemoveDuration(0);
        emptyView = LayoutInflater.from(this).inflate(R.layout.layout_common_empty, recyclerView, false);
        tvNone = emptyView.findViewById(R.id.tvEmpty);
        tvNone.setText("暂无关联物品");
        smrRf.setEnableLoadMore(false);
        smrRf.setEnableRefresh(false);
    }


    class ShopChoiceTypeAdapter extends BaseQuickAdapter<ReimbursementDetailEntity.GoodsInfosBean, BaseViewHolderEx> {
        private Activity context;

        public ShopChoiceTypeAdapter(int layoutResId, Activity context, @io.reactivex.annotations.Nullable List<ReimbursementDetailEntity.GoodsInfosBean> data) {
            super(layoutResId, data);
            this.context = context;
        }

        @Override
        protected void convert(BaseViewHolderEx helper, ReimbursementDetailEntity.GoodsInfosBean entity) {
            RelativeLayout rlType = helper.getView(R.id.rlType);
            RelativeLayout rlStatus = helper.getView(R.id.rlStatus);
            TextView tvTitle = helper.getView(R.id.tvTitle);
            TextView tvType = helper.getView(R.id.tvType);
            TextView tvInit = helper.getView(R.id.tvInit);
            RoundCornerImageView ivImg = helper.getView(R.id.ivImg);
            ImageView ivStatus = helper.getView(R.id.ivStatus);
            rlStatus.setVisibility(View.VISIBLE);
            tvType.setVisibility(View.GONE);
            tvInit.setText("所属人:" + entity.getOwnerEmployeeName() + "   所属项目组:" + entity.getGroupName());
            GlideUtils.load(context, entity.getImageUrl(), ivImg, R.drawable.default_news);
            String title = entity.getGoodsName() + " <font color='#999999'>" + entity.getGoodsCode() + "</font> " + ClearMoreZeroUtil.subZeroAndDot(entity.getAmt()) + "元";
            tvTitle.setText(Html.fromHtml(title));
            RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) ivStatus.getLayoutParams();
            //已选择物品
            params.height = Utils.dip2px(context, 24);
            params.width = Utils.dip2px(context, 24);
            ivStatus.setBackgroundResource(R.drawable.iv_delete_64);
            ivStatus.setLayoutParams(params);
            rlStatus.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    Map<String,String> map=new HashMap<>();
                    map.put("deleteShopPosition",helper.getItemPosition()+"");
                    EventBus.getDefault().post(new MyEvent(map));
                    list.remove(helper.getItemPosition());
                    adapter.notifyItemRemoved(helper.getItemPosition());
                    adapter.notifyItemRangeChanged(helper.getItemPosition(), list.size() - helper.getItemPosition());
                    if(list.size()==0){
                        adapter.setEmptyView(emptyView);
                        EventBus.getDefault().post(new MyEvent("deleteShopClear"));
                    }
                }
            });
            helper.getItemView().setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (Urls.antiShake.check()) {
                        return;
                    }
                    Intent intent = new Intent(context, ShopDetailActivity.class);
                    intent.putExtra("goodsCode", entity.getGoodsCode());
                    context.startActivity(intent);
                }
            });
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onMoonEvent(ShopEvent messageEvent) {
        //事件订阅者处理事件
        if (messageEvent.getList() != null) {
            list.clear();
            list.addAll(messageEvent.getList());
            adapter.notifyDataSetChanged();
        }
    }

    @OnClick({R.id.common_title_back, R.id.common_right_txt})
    public void onClick(View view) {
        Intent intent = null;
        switch (view.getId()) {
            case R.id.common_title_back:
                finish();
                break;
            case R.id.common_right_txt:
                intent = new Intent(this, ShopNormalActivity.class);
                intent.putExtra("data", (Serializable) list);
                startActivity(intent);
                break;
        }
    }
}
